﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Diagnostics;
using System.Data;
using System.Data.Common;

using System.Data.Caching;

namespace DistributedCacheDataProvider
{
    public class DistributedCacheConnection : DbConnection, IDbConnection, IDisposable
    {
        // just a single no-arg constructor.
		// there is no point in having one that takes a connection string
		// as there is no connection string!

        public DistributedCacheConnection() 
		{
			Debug.WriteLine("DistributedCacheConnection.constructor", "DistributedCacheConnection");
		}

		protected override DbTransaction BeginDbTransaction(IsolationLevel iso)
		{
			Debug.WriteLine("DistributedCacheConnection.BeginDbTransaction", "DistributedCacheConnection");
			return null;
		}

		/*
		public void Dispose()
		{
			Debug.WriteLine("DistributedCacheConnection.Dispose", "DistributedCacheConnection"); 
		}
		*/

		void NotSupported()
		{
			throw new System.NotSupportedException();
		}

		void NotImpl()
		{
			throw new System.NotImplementedException();
		}

		/*
		public override IDbTransaction BeginTransaction(IsolationLevel iso)
		{
			Debug.WriteLine("DistributedCacheConnection.BeginTransaction(iso)", "DistributedCacheConnection");
			NotSupported();
			return null;
		}

		public override IDbTransaction BeginTransaction()
		{
			Debug.WriteLine("DistributedCacheConnection.BeginTransaction()", "DistributedCacheConnection"); 
			NotSupported();
			return null;
		}
		*/

		public override void ChangeDatabase(string newdb)
		{
			Debug.WriteLine("DistributedCacheConnection.ChangeDatabase", "DistributedCacheConnection"); 
			NotSupported();
			return; 
		}

		public override void Close() 
		{
			Debug.WriteLine("DistributedCacheConnection.Close", "DistributedCacheConnection");
			_State = ConnectionState.Closed;
			return;
		}

		/*
		public override DbCommand CreateCommand()
		{
			Debug.WriteLine("DistributedCacheConnection.CreateCommand", "DistributedCacheConnection");
			IDbCommand idbCommand = (IDbCommand)(new DistributedCacheCommand());
			idbCommand.Connection = this;
			return idbCommand; 
		}
		*/

		protected override DbCommand CreateDbCommand()
		{
			Debug.WriteLine("DistributedCacheConnection.CreateDbCommand", "DistributedCacheConnection");
			DbCommand dbCommand = (DbCommand)(new DistributedCacheCommand());
			dbCommand.Connection = this;
			return dbCommand;
		}

		public override void Open()
		{ 
			Debug.WriteLine("DistributedCacheConnection.Open", "DistributedCacheConnection");
			_State = ConnectionState.Open;
		}
		
		// always return what the user set
		string _ConnectionString = "";
		public override string ConnectionString
		{
			get { 
				Debug.WriteLine("DistributedCacheConnection.get_ConnectionString", "DistributedCacheConnection");
				return _ConnectionString;
			}
			set { 
				Debug.WriteLine("DistributedCacheConnection.set_ConnectionString", "DistributedCacheConnection");
				_ConnectionString = value;
			}
		}

		int _ConnectionTimeout = 0;
		public override int ConnectionTimeout
		{
			get {
				Debug.WriteLine("DistributedCacheConnection.get_ConnectionTimeout", "DistributedCacheConnection"); 
				return _ConnectionTimeout;
			}
		}

		string _Database = "";
		public override string Database
		{
			get { 
				Debug.WriteLine("DistributedCacheConnection.get_Database", "DistributedCacheConnection");
				return _Database;
			}
		}

		string _DataSource = "";
		public override string DataSource
		{
			get
			{
				Debug.WriteLine("DistributedCacheConnection.get_DataSource", "DistributedCacheConnection");
				return _DataSource;
			}
		}

		string _ServerVersion = "";
		public override string ServerVersion
		{
			get
			{
				Debug.WriteLine("DistributedCacheConnection.get_ServerVersion", "DistributedCacheConnection");
				return _ServerVersion;
			}
		}

		ConnectionState _State = ConnectionState.Closed;
		public override ConnectionState State
		{
			get 
			{
				Debug.WriteLine("DistributedCacheConnection.get_ConnectionState", "DistributedCacheConnection"); 
				return _State;
			}
		}

        public override DataTable GetSchema()
        {
            Debug.WriteLine("DistributedCacheConnection.GetSchema()", "DistributedCacheConnection");

            return base.GetSchema();
        }

        public override DataTable GetSchema(string collectionName)
        {
            Debug.WriteLine("DistributedCacheConnection.GetSchema(string collectionName)", "DistributedCacheConnection");
            CacheFactory cacheCluster = new CacheFactory();
            Cache cache = cacheCluster.GetCache("TestCache");

            DataTable schema = new DataTable();
            DataRow row;
            
            schema.Columns.Add("Test Column");
            row = schema.NewRow();
            row[0] = "Test Column Value";
            schema.Rows.Add(row);
            return schema;
        }

        public override DataTable GetSchema(string collectionName, string[] restrictionValues)
        {
            Debug.WriteLine("DistributedCacheConnection.GetSchema(string collectionName, string[] restrictionValues)", "DistributedCacheConnection");
            return base.GetSchema(collectionName, restrictionValues);
        }
    }
}
